/*
 * Copyright (C) 2005-2006 Dmitri Maximovich <maxim@md.pp.ru>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */
var Options = {

  onLoad: function() {
  	var list = document.getElementById("servers");
  	Options.clear(list);
  	
  	var servers = P.getServersAsArray();
  	for(var i = 0; i < servers.length; i++) {
  	  var server = servers[i];

	  var item = list.appendItem(server.name);
	  item.value = server.id;
	  
	  var cellUrl = document.createElement("listcell");
	  cellUrl.setAttribute("label", server.url);
      item.appendChild(cellUrl);
  	}
  },

  // TODO: better?
  clear: function(list) {
  	var count = list.getRowCount();
  	for(var i = 0; i < count; i++) {
  	  list.removeItemAt(0);
  	}
  },
  
  onSave: function() {
  	//P.setString("a", ""+(new Date()));
  },

  onAddServerCommand: function() {
    window.openDialog("chrome://cc/content/addServer.xul", "", "centerscreen", null);
	// TODO: only if OK pressed
    Options.onLoad();
  },

  onEditServerCommand: function() {
	var list = document.getElementById("servers");
	var selected = list.selectedItem;
	if (selected != null) {
      var id = selected.value;
      window.openDialog("chrome://cc/content/addServer.xul", "", "centerscreen", id);
      // TODO: only if OK pressed
      Options.onLoad();
    }
  },
 
  onDeleteServerCommand: function() {
	var list = document.getElementById("servers");
	var selected = list.selectedItem;
	if (selected != null) {
      if (confirm("Are you sure you want to delete '"+selected.label+"'?")) {
        var id = selected.value;
	    P.deleteServer(id);
        Options.onLoad();
	  }
	}
  }

}
 